package cn.com.open.itembank.mapper;

import cn.com.open.itembank.dto.QuestionTypeManageDto;
import cn.com.open.itembank.entity.QuestionType;
import cn.com.open.itembank.entity.QuestionTypeUseRelation;
import cn.com.open.itembank.sqlprovider.QuestionTypeSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by lucky on 2017/8/14.
 */
@Mapper
public interface QuestionTypeMapper {

    /**
     * 根据题型id获取题型信息.
     *
     * @param id 题型id
     * @return 题型信息
     */
    @Select("SELECT * FROM QuestionTypeDefine WHERE QuestionTypeID = #{id}")
    QuestionType getById(@Param("id") String id);

    /**
     * 根据大题id获取题型信息.
     *
     * @param sectionId 大题id
     * @return 题型信息
     */
    @Select("SELECT * FROM QuestionTypeDefine WHERE SectionID = #{sectionId}")
    QuestionType getBySectionId(@Param("sectionId") String sectionId);

    /**
     * 根据题型id获取题型使用关系信息.
     *
     * @param id         题型id
     * @param itemBankId 题库id
     * @param appId      appid
     * @return 题型信息
     */
    @Select("SELECT * FROM QuestionTypeUseRelation WHERE QuestionTypeID = #{id} AND (ItemBankID=#{itemBankID} OR (AppID=#{appId} AND IsAppShare=1) OR IsDefault=1) AND Status<>-1")
    QuestionTypeUseRelation getRelationById(@Param("id") String id, @Param("itemBankID") String itemBankId, @Param("appId") String appId);

    /**
     * 根据内部题型id和大题序号获取数据条数.
     *
     * @param innerTypeId  内部题型ID
     * @param serialNumber 大题序号
     * @return 数据条数
     */
    @Select("SELECT COUNT(*) FROM QuestionTypeDefine WHERE QuestionInnerTypeID = #{innerTypeId} AND SerialNumber = #{serialNumber}")
    int getCountByInnerTypeIdAndSn(@Param("innerTypeId") int innerTypeId, @Param("serialNumber") int serialNumber);

    /**
     * 根据题型名称获取数据条数.
     *
     * @param name 题型名称
     * @return 数据条数
     */
    @Select("SELECT COUNT(*) FROM QuestionTypeDefine WHERE QuestionTypeName = #{name}")
    int getCountByName(@Param("name") String name);

    /**
     * 根据内部题型ID,题型ID和题型名称，判断是否存在数据.
     *
     * @param innerTypeId appKey
     * @param id          题型ID
     * @param name        题型名称
     * @return the int
     */
    @SelectProvider(type = QuestionTypeSqlProvider.class, method = "isExistQuestionTypeName")
    int isExistQuestionTypeName(@Param("innerTypeId") int innerTypeId, @Param("id") String id, @Param("name") String name);

    /**
     * Insert 题型信息.
     *
     * @param model 题型信息
     * @return the int
     */
    @InsertProvider(type = QuestionTypeSqlProvider.class, method = "insert")
    int insert(QuestionType model);

    /**
     * Insert 题型使用关系信息.
     *
     * @param model 题型使用关系信息
     * @return the int
     */
    @InsertProvider(type = QuestionTypeSqlProvider.class, method = "insertRelation")
    int insertRelation(QuestionTypeUseRelation model);

    /**
     * 修改题型名称.
     *
     * @param id         题型id
     * @param name       题型名称
     * @param directions 题型描述
     * @return the int
     */
    @Delete("UPDATE QuestionTypeDefine SET QuestionTypeName=#{name},Directions=#{directions} WHERE QuestionTypeID=#{id}")
    int update(@Param("id") String id, @Param("name") String name, @Param("directions") String directions);

    /**
     * 修改题型使用关系.
     *
     * @param id         题型id
     * @param itemBankID 题库id
     * @param appID      appid
     * @param isAppShare 是否app共享
     * @return the int
     */
    @Delete("UPDATE QuestionTypeUseRelation SET AppID=#{appID},IsAppShare=#{isAppShare} WHERE QuestionTypeID=#{id} AND ItemBankID=#{itemBankID}")
    int updateRelation(@Param("id") String id, @Param("itemBankID") String itemBankID, @Param("appID") String appID, @Param("isAppShare") int isAppShare);

    /**
     * 根据题型id获取关联试题数据条数.
     *
     * @param id         题型id
     * @param itemBankID 题库id
     * @return 数据条数
     */
    @Select("SELECT COUNT(*) FROM Question q INNER JOIN QuestionUseRelation qur ON q.QuestionID=qur.QuestionID WHERE QuestionTypeID=#{id} AND ItemBankID=#{itemBankID}")
    int getQuestionCount(@Param("id") String id, @Param("itemBankID") String itemBankID);

    /**
     * 修改题型使用关系状态（标记删除）.
     *
     * @param id         题型id
     * @param itemBankID 题库id
     * @return the int
     */
    @Delete("UPDATE QuestionTypeUseRelation SET Status=-1 WHERE QuestionTypeID=#{id} AND ItemBankID=#{itemBankID}")
    int deleteByItemBankId(@Param("id") String id, @Param("itemBankID") String itemBankID);

    /**
     * 恢复标记删除的修改题型使用关系状态.
     *
     * @param id         题型id
     * @param itemBankID 题库id
     * @return the int
     */
    @Delete("UPDATE QuestionTypeUseRelation SET Status = 1 WHERE QuestionTypeID=#{id} AND ItemBankID=#{itemBankID}")
    int deleteRollBackByItemBankId(@Param("id") String id, @Param("itemBankID") String itemBankID);

    /**
     * 根据题库ID获取题型列表.
     *
     * @param itemBankID 题库id
     * @param appID      appid
     * @param admin      是否管理页面用
     * @param name       题型名称模糊查询
     * @return the list by item bank id
     */
    @SelectProvider(type = QuestionTypeSqlProvider.class, method = "getListByItemBankId")
    List<QuestionType> getListByItemBankId(@Param("itemBankID") String itemBankID, @Param("appID") String appID, @Param("admin") boolean admin, @Param("name") String name);

    /**
     * 根据题库ID获取题型列表(admin).
     *
     * @param itemBankID 题库id
     * @param appID      appid
     * @param admin      是否管理页面用
     * @param name       题型名称模糊查询
     * @return the list by item bank id and name
     */
    @SelectProvider(type = QuestionTypeSqlProvider.class, method = "getListByItemBankId")
    List<QuestionTypeManageDto> getListByItemBankIdAndName(@Param("itemBankID") String itemBankID, @Param("appID") String appID, @Param("admin") boolean admin, @Param("name") String name);


    /**
     * 修改题型状态.
     *
     * @param id     题型id
     * @param status 题型新状态
     * @return the int
     */
    @Update("UPDATE QuestionTypeUseRelation SET Status=#{status} WHERE QuestionTypeID=#{id}")
    int changeStatus(@Param("id") String id, @Param("status") int status);

    /**
     * 根据题库ID获取题型信息
     * @param questionTypeID 题型标识
     * @return
     */
    @Select("SELECT * FROM QuestionTypeDefine WHERE QuestionTypeID = #{QuestionTypeID}")
    QuestionType getByQuestionTypeId(@Param("QuestionTypeID") String questionTypeID);

    /**
     * 根据题库内部题型标识获取题型信息
     * @param questionInnerTypeID 内部题型标识
     * @return
     */
    @Select("SELECT * FROM QuestionTypeDefine WHERE QuestionInnerTypeID = #{QuestionInnerTypeID}")
    QuestionType getByQuestionInnerTypeId(@Param("QuestionInnerTypeID") int questionInnerTypeID);
}
